<?php
namespace App\Models\ES;

use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\ClientBuilder;

class User extends ElasticBase
{
    const ROUTING = 'web';
    const USER_INDEX = "user_index";
    const USER_TABLE = "user_type";

    public $uid = 0;
    public $mobile = 0;
    public $username = '';

	public  function __construct()
	{
		parent::__construct();
	}

    protected function body()
    {
        return obj_to_array($this);
    }

    public function table()
    {
        return [
            'index'   => getenv("ELASTICSEARCH_PREFIX")."_".self::USER_INDEX,
            'type'    => self::USER_TABLE,
            'routing' => self::ROUTING,
//            'id'    => 0,
            'body'    => [],
        ];
    }

    public function create($arr = [ 'uid' => ['type' => 'text' ],'mobile' => ['type' => 'text'],'username' => ['type' => 'text']])
    {
        parent::create($arr);
    }

    public function get_list($keyword = '', $size = 10,$from = 0)
    {
        $result = [];
        $params['query']['bool']['should'] = array(
            array('match' => array('uid' => $keyword)),
            array('match' => array('name' => $keyword)),
            array('match' => array('mobile' => $keyword)),
        );
        $params['highlight'] = [
            'pre_tags' => ["<font color='#FF4855'>"],
            'post_tags' => ["</font>"],
            'fields' => [
                "uid" => new \stdClass(),
                "name" => new \stdClass(),
                "mobile" => new \stdClass(),
            ]
        ];
        $result = parent::search($params,$size,$from);
        return $result;
    }

    public function get_list_other($keyword = '', $size = 10,$from = 0){
        $list = $this->get_list($keyword, $size , $from);
        
        return $list;
    }

    public function getByUid($uid)
    {
        $params ['query']['match']['uid'] = (string)$uid;
        // print_r($params);exit();
        $s = [];
        try {
            $s = parent::search($params,1);
        } catch (Missing404Exception $e) {
          
        }
        return $s;
    }
}